# delimit ;  
set more 1 ;  
local useRAND 1;


drop _all;
clear all;
capture log close;
*set maxvar 32000;
set mem 1000m;



*global folder "C:\Users\uctpkp0\Desktop";

* global folder "C:\research";
*global folder "C:\Users\Ali\Documents";
*global folder "d:\Dropbox\";
*global folder "/Users/charlesarapakis/Desktop";
*global folder "C:\Users\Jeremy\Dropbox";
*global folder "~/";
global folder "C:\Users\ebf26\Dropbox";



         local saved  "$folder\hrs\merge\";
         local trackd "$folder\hrs\tracker\";
         local wave1d "$folder\hrs\wave1\";
         local wave2d "$folder\hrs\wave2\";
         local wave3d "$folder\hrs\wave3\";
         local wave4d "$folder\hrs\wave4\";
         local wave5d "$folder\hrs\wave5\";
		 local wave6d "$folder\hrs\wave6\";
         local wave7d "$folder\hrs\wave7\";
         local wave8d "$folder\hrs\wave8\";
		 local wave9d "$folder\hrs\wave9\";
		 local wave10d "$folder\hrs\wave10\";
		 local wave11d "$folder\hrs\wave11\";
		 local wave12d "$folder\hrs\wave12\";
		 local wave13d "$folder\hrs\wave13\";
		 local wave14d "$folder\hrs\wave14\";
		 local wave15d "$folder\hrs\wave15\";
         local aheadw1 "$folder\hrs\aheadw1\";
         local aheadw2 "$folder\hrs\aheadw2\";


* this is for getting the job codes for wave2 straight;
* this also recodes many variables;


log using "`saved'merge5.log", replace ;

use "`saved'hrs1";

cd `saved';
program define appendr;
        local i=2;
        while `i'<=llmaxwave[1] {;
		append using `saved'hrs`i'.dta; * comment this when using UNIX;
local i=`i'+1;
local i2 = RANDwaves[1]+1;
local i3 = lmaxwave[1]+1;
if `i' == `i2' {;
	local i = 102 ;
};

if `i' == `i3' {;
	local i = 202 ;
};

};
end;
appendr;

sum *med*;

/*
gen age1=age;
gen long indnum=HHID*1000+PN;

sort indnum age1;
gen test2 = string(indnum) + string(age1);
gen test3 = .;
replace test3 = 1 if test2[_n] == test2[_n-1];

gen test4=string(indnum) + string(wave);
gen test5 = .;
replace test5 = 1 if test4[_n] == test4[_n-1];


gen test6=string(HHID) + string(PN) + string(wave);
gen test7 = .;
replace test7 = 1 if test6[_n] == test6[_n-1];

egen globtest=max(test7), by (indnum);

sort globtest indnum wave;
order globtest indnum wave HHID PN age1 test*;
browse;
sum test*;
exit;

*/


***********************************************************************************;
* decide on RAND or Eric's old HRS coding                                          ;
***********************************************************************************;
gen useRAND=`useRAND';

drop if dump==1 & useRAND==0;
tab wave;
drop if rdump==1 & useRAND==1;
tab wave;
*pause HERE;

****************the RAND coding of married looks much better than the basic one;

gen temp=.;
replace temp=1 if rmarried ==1|rmarried==2;
replace temp=0 if rmarried>2 & rmarried<20;


tab temp married;
replace married=temp if temp~=.;
drop temp;

* married is missing for wave 6;
replace married=0 if married==. & rmarried~=1 & rmarried~=.;
replace married=1 if married==. & rmarried>2 & rmarried<20;


local i = 1;
while `i'<=14 {;
	gen wave`i'=0; replace wave`i'=1 if wave==`i';
	local i = `i' + 1;
};



local i = 2;
while `i'<=9 {;
	gen wave10`i'=0; replace wave10`i'=1 if wave==10`i';
	gen wave20`i'=0; replace wave20`i'=1 if wave==20`i';
	local i = `i' + 1;
};
local i = 10;
while `i'<=14 {;
	gen wave1`i'=0; replace wave1`i'=1 if wave==1`i';
	gen wave2`i'=0; replace wave2`i'=1 if wave==2`i';
	local i = `i' + 1;
};



*this is the original (before wave6) code, not sure if I should add something;
gen lfpr=0;
replace lfpr=1 if hours>1;
replace lfpr=0 if hours==0;
replace lfpr=. if hours==.;

gen lfprz=.;
gen lfprzz=.;
gen hoursz=.;
gen hourszz=.;

* check if changes in labor force participation rates across waves can be explained by age;
gen age2=age*age;
gen age3=age2*age;
reg lfpr age age2 age3 wave2 wave3 wave4 wave5 wave6 wave7 wave8 wave9;
drop age2 age3;

*generate birthyear dummies - this might be a fucked up way of doing it via realyear;
* realyear is not right for the AHEAD waves, but I can still sort on it;
gen realyear=.; 

replace realyear=92 if wave==1 ;
replace realyear=94 if wave==2 ;
replace realyear=96 if wave==3 ;

replace realyear=93 if wave==102 ;
replace realyear=95 if wave==103 ;

replace realyear= 92.5 if wave==202 ;
replace realyear= 94.5 if wave==203 ;
replace realyear=96.5 if wave==204 ;

replace realyear=93 if wave==1 &  (hrscoh==11|hrscoh==12);
replace realyear=95 if wave==2 &  (hrscoh==11|hrscoh==12);

replace realyear=94 if wave==102 &  (hrscoh==11|hrscoh==12);

replace realyear=93.5 if wave==202 &  (hrscoh==11|hrscoh==12); 
replace realyear=96 if wave==204 &  (hrscoh==11|hrscoh==12);

local i = 4;
local j = 0;
local k = 5;

while `i' <= 9 {;
	replace realyear=(98+`j') if wave==`i';
	replace realyear=(97+`j') if wave==10`i';
	replace realyear=(98.5+`j') if wave==20`k';
	local i = `i' + 1;
	local j = `j' + 2;
	local k = `k' + 1;
};
local i = 10;
local j = 12;
local k = 11;
while `i' <= 14 {;
	replace realyear=(98+`j') if wave==`i';
	replace realyear=(97+`j') if wave==1`i';
	replace realyear=(98.5+`j') if wave==2`k';
	local i = `i' + 1;
	local j = `j' + 2;
	local k = `k' + 1;
};

sort wave;
by wave: sum realyear;
*pause HERE;

*sum medcost oop* drugc iprem if hrscoh==11 & wave==2;


* generate exit (or hazard) rates;
sort HHID PN age;
gen haz=.;
replace haz=1 if lfpr==1 & lfpr[_n-1]==1 & HHID==HHID[_n-1] & PN==PN[_n-1];
replace haz=0 if lfpr==0 & lfpr[_n-1]==1 & HHID==HHID[_n-1] & PN==PN[_n-1];
* generate re-entry rates;
gen enter=.;
replace enter=1 if lfpr==1 & lfpr[_n-1]==0 & HHID==HHID[_n-1] & PN==PN[_n-1];
replace enter=0 if lfpr==0 & lfpr[_n-1]==0 & HHID==HHID[_n-1] & PN==PN[_n-1];
gen lnew=.;
replace lnew=1 if lfpr==1 & lfpr[_n-1]==. & HHID==HHID[_n-1] & PN==PN[_n-1];
replace lnew=0 if lfpr==0 & lfpr[_n-1]==. & HHID==HHID[_n-1] & PN==PN[_n-1];



* generate exit (or hazard) rates -- with the  Z vars;
/*sort HHID PN age;
gen hazz=.;
replace hazz=1 if lfprz==1 & lfprz[_n-1]==1 & HHID==HHID[_n-1] & PN==PN[_n-1];
replace hazz=0 if lfprz==0 & lfprz[_n-1]==1 & HHID==HHID[_n-1] & PN==PN[_n-1];
* generate re-entry rates;
gen enterz=.;
replace enterz=1 if lfprz==1 & lfprz[_n-1]==0 & HHID==HHID[_n-1] & PN==PN[_n-1];
replace enterz=0 if lfprz==0 & lfprz[_n-1]==0 & HHID==HHID[_n-1] & PN==PN[_n-1];
gen lnewz=.;
replace lnewz=1 if lfprz==1 & lfprz[_n-1]==. & HHID==HHID[_n-1] & PN==PN[_n-1];
replace lnewz=0 if lfprz==0 & lfprz[_n-1]==. & HHID==HHID[_n-1] & PN==PN[_n-1];
*/





sort wave;
by wave: summ s*;

* generate last period's marital status for those who have died;
gen lmarried=married;
sort HHID PN wave;
replace lmarried=married[_n-1] if HHID==HHID[_n-1] & PN==PN[_n-1] & wave<=RANDwaves;
sort HHID PN wave;
order HHID PN wave married lmarried dead male;
sort wave;

by wave: summ male assets dead if married==1;
by wave: summ male assets dead if lmarried==1;
by wave: summ male assets dead if lmarried==1 & male==1;
by wave: summ male assets dead if lmarried==1 & male==0;

by wave: summ male assets dead if married==1 & male==1;
by wave: summ male assets dead if married==1 & male==0;






if `useRAND'==0{;
*drop rdead;
*drop roopd;
*drop rtc;
drop rhosp;
drop rnursing;
drop rdrtimes;
rename rdruguse druguse; * this looks like a new and interesting variable;
drop routsurg;
drop rdentist;

*drop _roopd; 
drop rhouse;
* drop rmstat;
drop rmort;
drop rwheels;
drop rIRA;
drop rbus;
drop rexasset;
drop rdebts;
drop rnohouse;
drop rocc;
drop _rocc;
drop rind;
drop _rind;
drop _rhours;
*replace lfpr=rlfpr if lfpr==. & (wave==6|wave==7|wave==8|wave==9);
*replace heal=rheal if heal==. & (wave==6|wave==7|wave==8|wave==9);  
replace assets=rassets if assets==. & (wave==8|wave==9); * HRS does not impute missing asset data after wave 7;
drop rlfpr;
drop _rlfpr;
drop rweeks;
drop _rweeks;
drop rhours2job;
drop _rhours2job;
drop rweeks2job;
drop _rweeks2job;
drop rwage;
drop _rwage;
*drop reprfra;
drop rearn;
drop _rearn;
drop rLABY;
drop _rLABY;
drop rCAPY;
drop rPENY;
drop _rPENY;
drop rSOCY;
drop _rSOCY;
drop rOTHincY;
drop rOTHgovY;
drop rUNEMPY;
drop rSSDIY;
drop _rheal;
drop rhealch;
drop _rhealch;
drop _rdead;
drop rtenure;
drop _rtenure;
*drop rmedcare;
*drop reprhi;
*drop rinssp;
*drop rinspr;
*drop rrlage;
drop rhours;
drop rheal;
drop repins;
drop rinspriv;
drop rinsgov;
drop rliquid;
drop rstock;
drop rbonds;
drop rassets;
*drop rwork;
drop _rage;
drop rage;

gen medicaid=.; * Medicaid health insurance;
replace medicaid=0 if insgov==0|insgov==1;
replace medicaid=1 if insgov==2; 
replace insgov=0 if medicaid==1; * make insgov "Federal" insurance;


};

sort wave;
by wave: sum *tc;
by wave: sum *tc if dead==1;


if `useRAND'==1{;
* medical expense variables (note that this will keep the HRS codes for drugc, oop, and iprem);
replace dead =rdead if rdead~=.; * RAND seems to have a lot of missings for this variable; * use RAND coding if possible BELOW-->; 
replace oopd=roopd if roopd~=.;
replace _oopd=_roopd if _roopd~=.;
*replace tc =rtc;
replace hosp =rhosp;
replace nursing =rnursing if dead==0; * use RAND version of this variable for the living, my version for the dead;  
replace drtimes =rdrtimes;
gen druguse =rdruguse;
replace outsurg =routsurg;
replace dentist =rdentist;

replace medcare=rmedcare;
gen medicaid=rmedicaid;
replace insgov=rinsgov;



* asset variables;
replace house=rhouse;
replace mort=rmort;
replace remort=0; * RAND seems like its re variable is our re-remort variable;
replace wheels=rwheels;
replace IRA=rIRA;
replace liquid=rliquid;
replace stock=rstock;
replace bus=rbus;
replace bonds=rbonds;
replace exasset=rexasset;
replace debts=rdebts;
replace re=rre;
replace assets=rassets;
replace nohouse=rnohouse;

replace occ=rocc;
replace ind=rind;
replace lfpr=rlfpr;
replace lfprz=rlfprz;
replace lfprzz=rlfprzz;
replace wage=rwage;
replace earn=rearn;
replace _earn=_rearn;
gen _socy=_rSOCY;
gen _peny=_rPENY;

* income variables, which are a little tricky because there are differences between eric's coding and the rand coding;
replace laby=rLABY;
replace capy=rCAPY;
replace peny=rPENY;
replace socy=rSOCY;
gen OTHincY=rOTHincY;
gen OTHgovY=rOTHgovY;
gen UNEMPY=rUNEMPY;
gen SSDIY=rSSDIY;
replace faminc=rfaminc;
replace healch=rhealch;
replace _dead=_rdead;
replace tenure=rtenure;

replace hours=rhours if rhours~=.;
replace lfpr=.;
replace lfpr=0 if hours<300 & hours~=.;
replace lfpr=1 if hours>299 & hours~=.;

replace hoursz=rhoursz if rhoursz~=.;
replace hourszz=rhourszz if rhourszz~=.;
replace lfprz=.;
replace lfprz=0 if hoursz<300 & hoursz~=.;
replace lfprz=1 if hoursz>299 & hoursz~=.;

replace lfprzz=.;
replace lfprzz=0 if hourszz<300 & hourszz~=.;
replace lfprzz=1 if hourszz>299 & hourszz~=.;

replace heal=rheal;
* RAND insurance variables are still really messy;
replace epins=repins;
replace inspriv=rinspriv;
replace inssp=rinssp;
replace reprhi=1 if reprhi==2;
replace eprhi=reprhi;


replace _rage=_rage;
gen HRSage=age;
replace age=rage;

* replace eprhi=reprhi;
* replace inssp=rinssp;
* replace work=rwork;  * doesn't this variable exist in eric's coding? differences between rand and myself?;

* drop spousal rand variables (and some other variables) that do not exist in Eric's HRS coding;
drop _rocc;
drop _rind;
drop _rhours;
drop _rlfpr;
drop _rwage;
*drop _rheal;
gen _heal=_rheal;
drop _rhealch;
drop _rtenure;

* drop other rand variables that do not exist in Eric's HRS coding;
drop rweeks;
drop _rweeks;
drop rhours2job;
drop _rhours2job;
drop rweeks2job;
drop _rweeks2job;
*drop reprfra;
* drop rmedcare;
* drop rinspr;
* drop rrlage;

};

*************************************************************************************************************;
* impute medical expense variables for dead people in the RAND data;
replace roopd=oopd if roopd==. & dead==1;

replace _roopd=_oopd if _roopd==. & (_dead==1|married==0); * impute _roopd for dead people, for married people it  should be missing;
replace _oopd=_roopd if _oopd==. ;
replace rtc = tc if rtc==. & dead==1;
*Update following code for each new wave
* I did not code up oop medical costs in wave 8 in the HRS for those that are still living * use RAND coding;
replace oopd=roopd if (wave==8|wave==9|wave==10|wave==11|wave==12|wave==13|wave==14) & dead==0;



* health cost variables;
gen rmedcost=roopd+iprem;
replace medcost=rmedcost if medcost==.;
replace rmedcost=medcost if rmedcost==.;

gen _rmedcost=_roopd+_iprem;
replace _medcost=_rmedcost if _medcost==.;
replace _rmedcost=_medcost if _rmedcost==.;

* this effectively just means we are using the RAND data if useRAND==1;
replace medcost=oopd+iprem if oopd~=. & iprem~=.;
replace _medcost=_oopd+_iprem if _oopd~=. & _iprem~=.;

* mortality;
sort HHID PN realyear;
replace dead=dead[_n-1] if dead[_n-1]==1 &  HHID==HHID[_n-1] & PN==PN[_n-1];
replace rdead=rdead[_n-1] if rdead[_n-1]==1 & HHID==HHID[_n-1] & PN==PN[_n-1];

sort HHID PN wave;
gen died=1 if dead==1 & dead[_n-1]==0 & HHID==HHID[_n-1] & PN==PN[_n-1];

gen omPN=1-PN;
gen female=1-male;

sort HHID PN wave;
replace sub2w2=0 if died==1 & wave==2;

replace sub3w3=sub2w2 if died==1 & wave==3;
replace sub3w3=0 if sub2w2==. & died==1 & wave==3;

replace sub4w4=sub3w3 if died==1 & wave==4;
replace sub4w4=sub2w2 if sub4w4==. & died==1 & wave==4;

replace sub5w5=sub4w4 if died==1 & wave==5;
replace sub5w5=sub3w3 if sub5w5==. & died==1 & wave==5;
*replace sub5w5=sub2w2 if sub5w5==. & died==1 & wave==5;

replace sub6w6=sub5w5 if died==1 & wave==6;
replace sub6w6=sub4w4 if sub6w6==. & died==1 & wave==6;

replace sub7w7=sub6w6 if died==1 & wave==7;
replace sub7w7=sub5w5 if sub7w7==. & died==1 & wave==7;
*replace sub7w7=sub6w6 if rdead==1 & wave==7;
*replace sub7w7=sub5w5 if sub7w7==. & rdead==1 & wave==7;

replace sub8w8=sub7w7 if died==1 & wave==8;
replace sub8w8=sub6w6 if sub8w8==. & died==1 & wave==8;
*replace sub8w8=sub7w7 if rdead==1 & wave==8;
*replace sub8w8=sub6w6 if sub8w8==. & rdead==1 & wave==8;

replace sub9w9=sub8w8 if died==1 & wave==9;
replace sub9w9=sub7w7 if sub9w9==. & died==1 & wave==9;
*replace sub9w9=sub8w8 if rdead==1 & wave==9;
*replace sub9w9=sub7w7 if sub9w9==. & rdead==1 & wave==9;

replace sub10w10=sub9w9 if died==1 & wave==10;
replace sub10w10=sub8w8 if sub10w10==. & died==1 & wave==10;
*replace sub10w10=sub9w9 if rdead==1 & wave==10;
*replace sub10w10=sub8w8 if sub10w10==. & rdead==1 & wave==10;

replace sub11w11=sub10w10 if died==1 & wave==11;
replace sub11w11=sub9w9 if sub11w11==. & died==1 & wave==11;

replace sub12w12=sub11w11 if died==1 & wave==12;
replace sub12w12=sub10w10 if sub12w12==. & died==1 & wave==12;

replace sub13w13=sub12w12 if died==1 & wave==13;
replace sub13w13=sub11w11 if sub13w13==. & died==1 & wave==13;

replace sub14w14=sub13w13 if died==1 & wave==14;
replace sub14w14=sub12w12 if sub14w14==. & died==1 & wave==14;

gen sub=0;
local i = 1;
while `i'<=14{;
	replace sub=sub`i'w`i' if wave==`i';
	local i = `i' + 1;
};

egen group= group(HHID wave sub);
bysort group: gen num=_N;

replace sub=99 if num==3 & died==1 & wave<20;
drop group num;



* mortality of spouse seems to be screwed up;
gen oneMPN=1-PN;
sort HHID wave male PN;
replace _dead=dead[_n-1] if  HHID==HHID[_n-1] & sub==sub[_n-1] & wave==wave[_n-1];
sort HHID wave female omPN;
replace _dead=dead[_n-1] if  HHID==HHID[_n-1] & sub==sub[_n-1] & wave==wave[_n-1];

if `useRAND'==1{;
* impute some spousal variables;
gen _druguse=.;

sort HHID wave sub;
replace _tc=tc[_n-1] if  HHID==HHID[_n-1] & sub==sub[_n-1] & wave==wave[_n-1];
gen _rtc=.;
replace _rtc=rtc[_n-1] if  HHID==HHID[_n-1] & sub==sub[_n-1] & wave==wave[_n-1];

replace _hosp=hosp[_n-1] if HHID==HHID[_n-1] & sub==sub[_n-1] & wave==wave[_n-1];
replace _nursing=nursing[_n-1] if HHID==HHID[_n-1] & sub==sub[_n-1] & wave==wave[_n-1];
replace _drtimes=drtimes[_n-1] if HHID==HHID[_n-1] & sub==sub[_n-1] & wave==wave[_n-1];
*replace _druguse=durguse[_n-1] if HHID==HHID[_n-1] & sub==sub[_n-1] & wave==wave[_n-1];
replace _outsurg=outsurg[_n-1] if HHID==HHID[_n-1] & sub==sub[_n-1] & wave==wave[_n-1];
replace _dentist=dentist[_n-1] if HHID==HHID[_n-1] & sub==sub[_n-1] & wave==wave[_n-1];
replace _medhmo=medhmo[_n-1] if HHID==HHID[_n-1] & sub==sub[_n-1] & wave==wave[_n-1];
gen _medicaid=.;
replace _medicaid=medicaid[_n-1] if HHID==HHID[_n-1] & sub==sub[_n-1] & wave==wave[_n-1];
gen _dmedicaid=.;
replace _dmedicaid=dmedicaid[_n-1] if HHID==HHID[_n-1] & sub==sub[_n-1] & wave==wave[_n-1];
replace _medcare=medcare[_n-1] if HHID==HHID[_n-1] & sub==sub[_n-1] & wave==wave[_n-1];
replace _medcost=medcost[_n-1] if HHID==HHID[_n-1] & sub==sub[_n-1] & wave==wave[_n-1];

sort HHID wave sub;
replace _tc=tc[_n+1] if  HHID==HHID[_n+1] & sub==sub[_n+1] & wave==wave[_n+1];
replace _rtc=rtc[_n+1] if  HHID==HHID[_n+1] & sub==sub[_n+1] & wave==wave[_n+1];

replace _hosp=hosp[_n+1] if HHID==HHID[_n+1] & sub==sub[_n+1] & wave==wave[_n+1];
replace _nursing=nursing[_n+1] if HHID==HHID[_n+1] & sub==sub[_n+1] & wave==wave[_n+1];
replace _drtimes=drtimes[_n+1] if HHID==HHID[_n+1] & sub==sub[_n+1] & wave==wave[_n+1];
*replace _druguse=durguse[_n+1] if HHID==HHID[_n+1] & sub==sub[_n+1] & wave==wave[_n+1];
replace _outsurg=outsurg[_n+1] if HHID==HHID[_n+1] & sub==sub[_n+1] & wave==wave[_n+1];
replace _dentist=dentist[_n+1] if HHID==HHID[_n+1] & sub==sub[_n+1] & wave==wave[_n+1];
replace _medhmo=medhmo[_n+1] if HHID==HHID[_n+1] & sub==sub[_n+1] & wave==wave[_n+1];
replace _medicaid=medicaid[_n+1] if HHID==HHID[_n+1] & sub==sub[_n+1] & wave==wave[_n+1];
replace _dmedicaid=dmedicaid[_n+1] if HHID==HHID[_n+1] & sub==sub[_n+1] & wave==wave[_n+1];
replace _medcare=medcare[_n+1] if HHID==HHID[_n+1] & sub==sub[_n+1] & wave==wave[_n+1];
replace _medcost=medcost[_n+1] if HHID==HHID[_n+1] & sub==sub[_n+1] & wave==wave[_n+1];
};

if `useRAND'==0{;
gen _rtc=.;
gen _medicaid=.;
gen _dmedicaid=.;
gen _druguse=.;
gen _peny=.;
gen _socy=.;
};

drop omPN female;

*assigning exit medicaid to medicaid;
sort HHID PN wave;
replace medicaid= dmedicaid if medicaid==.;
replace medicaid=1 if medicaid==9 & medicaid[_n-1]==1 & HHID==HHID[_n-1] & PN==PN[_n-1];
replace medicaid=0 if medicaid==9 & medicaid[_n-1]==0 & HHID==HHID[_n-1] & PN==PN[_n-1];
replace medicaid=. if medicaid==9;


replace _medicaid=_dmedicaid if _medicaid==.;
replace _medicaid=1 if _medicaid==9 & _medicaid[_n-1]==1 & HHID==HHID[_n-1] & PN==PN[_n-1];
replace _medicaid=0 if _medicaid==9 & _medicaid[_n-1]==0 & HHID==HHID[_n-1] & PN==PN[_n-1];
replace _medicaid=. if _medicaid==9;








* make everything in real terms;
* used to have everything in 1998 $: now in 2005 $;
gen pce=.;
*replace pce=0.914 if realyear==92|realyear==92.5;
*replace pce=0.939 if realyear==93|realyear==93.5;
*replace pce=0.959 if realyear==94|realyear==94.5;
*replace pce=0.980 if realyear==95|realyear==95.5;
*replace pce=1.000 if realyear==96|realyear==96.5;
*replace pce=1.017 if realyear==97|realyear==97.5;
*replace pce=1.026 if realyear==98|realyear==98.5;
*replace pce=1.043 if realyear==99|realyear==99.5;
*replace pce=1.069 if realyear==100|realyear==100.5;
*replace pce=1.091 if realyear==101|realyear==101.5;
*replace pce=1.107 if realyear==102|realyear==102.5;
*replace pce=1.128 if realyear==103|realyear==103.5;
*replace pce=1.157 if realyear==104|realyear==104.5;
*replace pce=1.191 if realyear==105|realyear==105.5;
*replace pce=1.249 if realyear==106|realyear==106.5;
*replace pce=pce/1.026; * make pce =1.00 in 1998;


/* OLD PCE (from 2012,using 2005 base year)
* prices updated as of Jan 24, 2012;
replace pce=76.990 if realyear==92|realyear==92.5;
replace pce=78.679 if realyear==93|realyear==93.5;
replace pce=80.302 if realyear==94|realyear==94.5;
replace pce=82.078 if realyear==95|realyear==95.5;
replace pce=83.864 if realyear==96|realyear==96.5;
replace pce=85.433 if realyear==97|realyear==97.5;
replace pce=86.246 if realyear==98|realyear==98.5;
replace pce=87.636 if realyear==99|realyear==99.5;
replace pce=89.818 if realyear==100|realyear==100.5;
replace pce=91.530 if realyear==101|realyear==101.5;
replace pce=92.778 if realyear==102|realyear==102.5;
replace pce=94.659 if realyear==103|realyear==103.5;
replace pce=97.121 if realyear==104|realyear==104.5;
replace pce=100.000 if realyear==105|realyear==105.5;
replace pce=102.723 if realyear==106|realyear==106.5;
replace pce=105.499 if realyear==107|realyear==107.5;
replace pce=108.943 if realyear==108|realyear==108.5;
replace pce=109.169 if realyear==109|realyear==109.5;
replace pce=111.112 if realyear==110|realyear==110.5;
replace pce=pce/100; * make pce =1.00 in 2005;
*/;

* PCE updated Aug 2017, using 2014 base year;
replace pce=65.733147 if realyear==92|realyear==92.5;
replace pce=67.37431503 if realyear==93|realyear==93.5;
replace pce=68.77551396 if realyear==94|realyear==94.5;
replace pce=70.20337612 if realyear==95|realyear==95.5;
replace pce=71.69743665 if realyear==96|realyear==96.5;
replace pce=72.93497849 if realyear==97|realyear==97.5;
replace pce=73.4949064 if realyear==98|realyear==98.5;
replace pce=74.57430768 if realyear==99|realyear==99.5;
replace pce=76.43245927 if realyear==100|realyear==100.5;
replace pce=77.90813137 if realyear==101|realyear==101.5;
replace pce=78.95351403 if realyear==102|realyear==102.5;
replace pce=80.51561178 if realyear==103|realyear==103.5;
replace pce=82.47489978 if realyear==104|realyear==104.5;
replace pce=84.82678092 if realyear==105|realyear==105.5;
replace pce=87.09591409 if realyear==106|realyear==106.5;
replace pce=89.27678276 if realyear==107|realyear==107.5;
replace pce=92.00194917 if realyear==108|realyear==108.5;
replace pce=91.94218675 if realyear==109|realyear==109.5;
replace pce=93.4619911 	if realyear==110|realyear==110.5;
replace pce=95.75686808 if realyear==111|realyear==111.5;
replace pce=97.51572211 if realyear==112|realyear==112.5;
replace pce=98.68430731 if realyear==113|realyear==113.5;
replace pce=100 		if realyear==114|realyear==114.5;
replace pce=100.35 		if realyear==115|realyear==115.5;
replace pce=101.44 		if realyear==116|realyear==116.5;
* these below updated Sep 2022;
* taken from https://fred.stlouisfed.org/series/PCE ;

replace pce=103.07458   if realyear==117|realyear==117.5;
replace pce=105.278		if realyear==118|realyear==118.5;
replace pce=106.836		if realyear==119|realyear==119.5;
replace pce=108.092		if realyear==120|realyear==120.5;
replace pce=112.273		if realyear==121|realyear==121.5;

replace pce=pce/100; * make pce =1.00 in 2014;



* individual wage;
replace wage=wage/pce;

* asset variables;
replace assets=assets/pce;
replace liquid=liquid/pce;
replace stock=stock/pce;
replace bonds=bonds/pce;
replace re=re/pce;
replace wheels=wheels/pce;
replace exasset=exasset/pce;
replace debts=debts/pce;
replace bus=bus/pce;
replace house=house/pce;
replace nohouse=nohouse/pce;
replace IRA=IRA/pce;
replace remort=remort/pce;
replace mort=mort/pce;
replace trust=trust/pce;

* estates and death expense variables;
replace estate=estate/pce;
replace deathex=deathex/pce;
replace deathins=deathins/pce;
replace inspay=inspay/pce;
replace amspouse=amspouse/pce;

*pension wealth variables;
replace penwDCcurr=penwDCcurr/pce;
replace penwDClast=penwDClast/pce; 
replace penwDCprev=penwDCprev/pce; 
replace wagelast=wagelast/pce;
replace wageprev=wageprev/pce;
replace earnlast=earnlast/pce;
replace earnprev=earnprev/pce;


* household income variables;
replace faminc=faminc/pce;
replace laby=laby/pce;
replace capy=capy/pce;
replace othy=othy/pce;
replace peny=peny/pce;
replace socy=socy/pce;
replace earn=earn/pce;
replace SSIinc=SSIinc/pce;
replace WELFAREinc=WELFAREinc/pce;
replace VAinc=VAinc/pce;

replace selfy=selfy/pce if useRAND==0;
if useRAND==1 {;
replace SSDIY=SSDIY/pce;
replace UNEMPY=UNEMPY/pce;
replace OTHgovY=OTHgovY/pce;
replace OTHincY=OTHincY/pce;
replace rwage=rwage/pce;

};

* some individual income variables;
replace rSSDIY=rSSDIY/pce;
replace _rSSDIY=_rSSDIY/pce;
replace rSOCY=rSOCY/pce;
replace _rSOCY=_rSOCY/pce;
replace rPENY=rPENY/pce;
replace _rPENY=_rPENY/pce;


* health cost variables;
replace tc=tc/pce;
replace oop=oop/pce;
replace oopd=oopd/pce;
replace iprem=iprem/pce;
replace medhmo=medhmo/pce;
replace drugc=drugc/pce;
replace medcost=medcost/pce;

****************************************************************;
* subcomponents of medical expenses here                       *;
* these and drugc are now taken from randhrsimp1992_2018v2.dta *;                 *;
****************************************************************;
replace hospc=hospc/pce;
replace nursc=nursc/pce;
replace docc=docc/pce;
replace hhsc=hhsc/pce;
replace othels=othels/pce;
replace dentc=dentc/pce;
replace outsgc=outsgc/pce;

* added Chris OOP;
replace _hospc=_hospc/pce;
replace _nursc=_nursc/pce;
replace _docc=_docc/pce;
replace _hhsc=_hhsc/pce;
replace _othels=_othels/pce;
replace _dentc=_dentc/pce;
replace _outsgc=_outsgc/pce;

replace rtc=rtc/pce;
replace roopd=roopd/pce;
replace rmedcost=rmedcost/pce;

* spousal variables;
replace _oop=_oop/pce;
replace _tc=_tc/pce;
replace _oopd=_oopd/pce;
replace _iprem=_iprem/pce;
replace _medhmo=_medhmo/pce;
replace _drugc=_drugc/pce;
replace _medcost=_medcost/pce;
replace _earn=_earn/pce;

replace _peny=_peny/pce;
replace _socy=_socy/pce;

replace _rtc=_rtc/pce;
replace _roopd=_roopd/pce;
replace _rmedcost=_rmedcost/pce;


* different measures of wealth;
gen vla= liquid+stock+bonds;
gen nhnbw=vla+re+wheels+exasset-debt-remort;
gen totass=re+wheels+liquid+IRA+stock+bus+ bonds+exasset-debts-remort;
replace totass=totass+house-mort ;
sum assets totass if (assets~=. & totass~=.);
drop totass;
gen totass=nhnbw+bus+IRA+house-mort;
summ assets totass if (assets~=. & totass~=.);
*drop totass;

sum assets nhnbw vla;

******* Fix ages *****************************;
replace age=2006-birthyr if wave==9 & age==.;
replace age=2008-birthyr if wave==9 & age==.;
replace age=2010-birthyr if wave==10 & age==.;
replace age=2012-birthyr if wave==11 & age==.;
replace age=2014-birthyr if wave==12 & age==.;
replace age=2016-birthyr if wave==13 & age==.;
replace age=2018-birthyr if wave==14 & age==.;

********top and bottom code wages ************;

replace wage=. if wage<3;
replace wage=. if wage>200;

replace wageprev=. if wageprev<3;
replace wageprev=. if wageprev>200;

replace wagelast=. if wagelast<3;
replace wagelast=. if wagelast>200;

*impute Medicare part B premium;


gen ptBprem=.;
replace ptBprem= 31.8*12 if realyear==92;
replace ptBprem= 36.6*12 if realyear==93;
replace ptBprem= 41.1*12 if realyear==94;
replace ptBprem= 46.1*12 if realyear==95;
replace ptBprem= 42.5*12 if realyear==96;
replace ptBprem= 43.8*12 if realyear==98;
replace ptBprem= 45.5*12 if realyear==100 ;
replace ptBprem= 54*12 if realyear==102 ;
replace ptBprem= 66.6*12 if realyear==104 ;
replace ptBprem= 88.5*12 if realyear==106 ;



sort HHID PN wave;
gen faminc_nom=faminc*pce; //use nominal income for part B premium income brackets starting in 2008;
replace faminc_nom=faminc[_n-1]*pce if faminc_nom==. & HHID==HHID[_n-1] & PN==PN[_n-1];//don't have faminc for period of death, so use last round's

*2008, 2010, and later break down premia by family income and filing status;
replace ptBprem = 96.4*12 if realyear==108 & (faminc_nom<=85000 | faminc_nom==.) & married==0;
replace ptBprem = 122.2*12 if realyear==108 & faminc_nom>85000 & faminc_nom<=102000	& married==0;
replace ptBprem = 160.9*12 if realyear==108 & faminc_nom>102000 & faminc_nom<=153000 & married==0;
replace ptBprem = 199.7*12 if realyear==108 & faminc_nom>153000 & faminc_nom<=205000 & married==0;
replace ptBprem = 238.4*12 if realyear==108 & faminc_nom>205000 & faminc_nom<. & married==0 ;
replace ptBprem = 96.4*12 if realyear==108 & (faminc_nom<=164000|faminc_nom==.) & married==1;
replace ptBprem = 122.2*12 if realyear==108 & faminc_nom>164000 & faminc_nom<=204000 & married==1;
replace ptBprem = 160.9*12 if realyear==108 & faminc_nom>204000 & faminc_nom<=306000 & married==1;
replace ptBprem = 199.7*12 if realyear==108 & faminc_nom>306000 & faminc_nom<=410000 & married==1;
replace ptBprem = 238.4*12 if realyear==108 & faminc_nom>410000 & faminc_nom<. & married==1 ;

replace ptBprem = 110.5*12 if realyear==110 & (faminc_nom<=85000 | faminc_nom==.) & married==0;
replace ptBprem = 154.7*12 if realyear==110 & faminc_nom>85000 & faminc_nom<=107000	& married==0;
replace ptBprem = 221*12 if realyear==110 & faminc_nom>107000 & faminc_nom<=160000 & married==0;
replace ptBprem = 287.3*12 if realyear==110 & faminc_nom>160000 & faminc_nom<=214000 & married==0;
replace ptBprem = 353.6*12 if realyear==110 & faminc_nom>214000 & faminc_nom<. & married==0 ;
replace ptBprem = 110.5*12 if realyear==110 & (faminc_nom<=170000|faminc_nom==.) & married==1;
replace ptBprem = 154.7*12 if realyear==110 & faminc_nom>170000 & faminc_nom<=214000 & married==1;
replace ptBprem = 221*12 if realyear==110 & faminc_nom>214000 & faminc_nom<=320000 & married==1;
replace ptBprem = 287.3*12 if realyear==110 & faminc_nom>320000 & faminc_nom<=428000 & married==1;
replace ptBprem = 353.6*12 if realyear==110 & faminc_nom>428000 & faminc_nom<. & married==1 ;

replace ptBprem = 99.9*12 if realyear==112 & (faminc_nom<=85000 | faminc_nom==.) & married==0;
replace ptBprem = 139.9*12 if realyear==112 & faminc_nom>85000 & faminc_nom<=107000	& married==0;
replace ptBprem = 199.8*12 if realyear==112 & faminc_nom>107000 & faminc_nom<=160000 & married==0;
replace ptBprem = 259.7*12 if realyear==112 & faminc_nom>160000 & faminc_nom<=214000 & married==0;
replace ptBprem = 319.7*12 if realyear==112 & faminc_nom>214000 & faminc_nom<. & married==0 ;
replace ptBprem = 99.9*12 if realyear==112 & (faminc_nom<=170000|faminc_nom==.) & married==1;
replace ptBprem = 139.9*12 if realyear==112 & faminc_nom>170000 & faminc_nom<=214000 & married==1;
replace ptBprem = 199.8*12 if realyear==112 & faminc_nom>214000 & faminc_nom<=320000 & married==1;
replace ptBprem = 259.7*12 if realyear==112 & faminc_nom>320000 & faminc_nom<=428000 & married==1;
replace ptBprem = 319.7*12 if realyear==112 & faminc_nom>428000 & faminc_nom<. & married==1 ;

replace ptBprem = 104.9*12 if realyear==114 & (faminc_nom<=85000 | faminc_nom==.) & married==0;
replace ptBprem = 146.9*12 if realyear==114 & faminc_nom>85000 & faminc_nom<=107000	& married==0;
replace ptBprem = 209.8*12 if realyear==114 & faminc_nom>107000 & faminc_nom<=160000 & married==0;
replace ptBprem = 272.7*12 if realyear==114 & faminc_nom>160000 & faminc_nom<=214000 & married==0;
replace ptBprem = 335.7*12 if realyear==114 & faminc_nom>214000 & faminc_nom<. & married==0 ;
replace ptBprem = 104.9*12 if realyear==114 & (faminc_nom<=170000|faminc_nom==.) & married==1;
replace ptBprem = 146.9*12 if realyear==114 & faminc_nom>170000 & faminc_nom<=214000 & married==1;
replace ptBprem = 209.8*12 if realyear==114 & faminc_nom>214000 & faminc_nom<=320000 & married==1;
replace ptBprem = 272.7*12 if realyear==114 & faminc_nom>320000 & faminc_nom<=428000 & married==1;
replace ptBprem = 335.7*12 if realyear==114 & faminc_nom>428000 & faminc_nom<. & married==1 ;

replace ptBprem = 121.8*12 if realyear==116 & (faminc_nom<=85000 | faminc_nom==.) & married==0;
replace ptBprem = 121.8*12 if realyear==116 & (faminc_nom<=170000|faminc_nom==.) & married==1;
replace ptBprem = 170.5*12 if realyear==116 & faminc_nom>85000 & faminc_nom<=107000 & married==0;
replace ptBprem = 170.5*12 if realyear==116 & faminc_nom>170000 & faminc_nom<=214000 & married==1;
replace ptBprem = 243.6*12 if realyear==116 & faminc_nom>214000 & faminc_nom<=320000 & married==1;
replace ptBprem = 243.6*12 if realyear==116 & faminc_nom>107000 & faminc_nom<=160000 & married==0;
replace ptBprem = 316.7*12 if realyear==116 & faminc_nom>160000 & faminc_nom<=214000 & married==0;
replace ptBprem = 316.7*12 if realyear==116 & faminc_nom>320000 & faminc_nom<=428000 & married==1;
replace ptBprem = 389.8*12 if realyear==116 & faminc_nom>214000 & faminc_nom<. & married==0 ;
replace ptBprem = 389.8*12 if realyear==116 & faminc_nom>428000 & faminc_nom<. & married==1 ;

* Chris: 117-120 added Sep 2022. I've stored the source for these in folder merge/PCE and Part B Premiums;
replace ptBprem = 134.00*12 if realyear==117 & (faminc_nom<=85000 | faminc_nom==.) & married==0;
replace ptBprem = 134.00*12 if realyear==117 & (faminc_nom<=170000|faminc_nom==.) & married==1;
replace ptBprem = 187.50*12 if realyear==117 & faminc_nom>85000 & faminc_nom<=107000 & married==0;
replace ptBprem = 187.50*12 if realyear==117 & faminc_nom>170000 & faminc_nom<=214000 & married==1;
replace ptBprem = 267.90*12 if realyear==117 & faminc_nom>107000 & faminc_nom<=160000 & married==0;
replace ptBprem = 267.90*12 if realyear==117 & faminc_nom>214000 & faminc_nom<=320000 & married==1;
replace ptBprem = 348.30*12 if realyear==117 & faminc_nom>160000 & faminc_nom<=214000 & married==0;
replace ptBprem = 348.30*12 if realyear==117 & faminc_nom>320000 & faminc_nom<=428000 & married==1;
replace ptBprem = 428.60*12 if realyear==117 & faminc_nom>214000 & faminc_nom<. & married==0 ;
replace ptBprem = 428.60*12 if realyear==117 & faminc_nom>428000 & faminc_nom<. & married==1 ;

*  the bands changed but not the premia for 2018;
replace ptBprem = 134.00*12 if realyear==118 & (faminc_nom<=85000 | faminc_nom==.) & married==0;
replace ptBprem = 187.50*12 if realyear==118 & faminc_nom>85000 & faminc_nom<=107000 & married==0;
replace ptBprem = 267.90*12 if realyear==118 & faminc_nom>107000 & faminc_nom<=133500 & married==0;
replace ptBprem = 348.30*12 if realyear==118 & faminc_nom>133500 & faminc_nom<=160000 & married==0;
replace ptBprem = 428.60*12 if realyear==118 & faminc_nom>160000 & faminc_nom<. & married==0 ;

replace ptBprem = 134.00*12 if realyear==118 & (faminc_nom<=170000|faminc_nom==.) & married==1;
replace ptBprem = 187.50*12 if realyear==118 & faminc_nom>170000 & faminc_nom<=214000 & married==1;
replace ptBprem = 267.90*12 if realyear==118 & faminc_nom>214000 & faminc_nom<=267000 & married==1;
replace ptBprem = 348.30*12 if realyear==118 & faminc_nom>267000 & faminc_nom<=320000 & married==1;
replace ptBprem = 428.60*12 if realyear==118 & faminc_nom>320000 & faminc_nom<. & married==1 ;

*  a new higher band was introduced for 2019;
replace ptBprem = 135.50*12 if realyear==119 & (faminc_nom<=85000 | faminc_nom==.) & married==0;
replace ptBprem = 189.60*12 if realyear==119 & faminc_nom>85000 & faminc_nom<=107000 & married==0;
replace ptBprem = 270.90*12 if realyear==119 & faminc_nom>107000 & faminc_nom<=133500 & married==0;
replace ptBprem = 352.20*12 if realyear==119 & faminc_nom>133500 & faminc_nom<=160000 & married==0;
replace ptBprem = 433.40*12 if realyear==119 & faminc_nom>160000 & faminc_nom<=500000 & married==0;
replace ptBprem = 460.50*12 if realyear==119 & faminc_nom>500000 & faminc_nom<. & married==0 ;

replace ptBprem = 135.50*12 if realyear==119 & (faminc_nom<=170000|faminc_nom==.) & married==1;
replace ptBprem = 189.60*12 if realyear==119 & faminc_nom>170000 & faminc_nom<=214000 & married==1;
replace ptBprem = 270.90*12 if realyear==119 & faminc_nom>214000 & faminc_nom<=267000 & married==1;
replace ptBprem = 352.20*12 if realyear==119 & faminc_nom>267000 & faminc_nom<=320000 & married==1;
replace ptBprem = 433.40*12 if realyear==119 & faminc_nom>320000 & faminc_nom<=750000 & married==1;
replace ptBprem = 460.50*12 if realyear==119 & faminc_nom>750000 & faminc_nom<. & married==1 ;

* bands and premia changed in 2020;
replace ptBprem = 144.60*12 if realyear==120 & (faminc_nom<=87000 | faminc_nom==.) & married==0;
replace ptBprem = 202.40*12 if realyear==120 & faminc_nom>87000 & faminc_nom<=109000 & married==0;
replace ptBprem = 289.20*12 if realyear==120 & faminc_nom>109000 & faminc_nom<=136000 & married==0;
replace ptBprem = 376.00*12 if realyear==120 & faminc_nom>136000 & faminc_nom<=163000 & married==0;
replace ptBprem = 462.70*12 if realyear==120 & faminc_nom>163000 & faminc_nom<=500000 & married==0;
replace ptBprem = 491.60*12 if realyear==120 & faminc_nom>500000 & faminc_nom<. & married==0 ;

replace ptBprem = 144.60*12 if realyear==120 & (faminc_nom<=174000|faminc_nom==.) & married==1;
replace ptBprem = 202.40*12 if realyear==120 & faminc_nom>174000 & faminc_nom<=218000 & married==1;
replace ptBprem = 289.20*12 if realyear==120 & faminc_nom>218000 & faminc_nom<=272000 & married==1;
replace ptBprem = 376.00*12 if realyear==120 & faminc_nom>272000 & faminc_nom<=326000 & married==1;
replace ptBprem = 462.70*12 if realyear==120 & faminc_nom>326000 & faminc_nom<=750000 & married==1;
replace ptBprem = 491.60*12 if realyear==120 & faminc_nom>750000 & faminc_nom<. & married==1 ;

drop faminc_nom;

replace ptBprem=ptBprem/pce;

*if not in last period of life: ;
replace medcost=rmedcost if medcost==.; 
gen medcost_b=medcost+ptBprem if medicaid==0 & (medhmo==0|medhmo==.) & medcare==1; //medcost w/ part B if on medicare, but neither medicaid nor medicare HMO;
replace medcost_b=medcost if medcost_b==. & dead==0; 
gen iprem_b=iprem+ptBprem if medicaid==0 & (medhmo==0|medhmo==.) & medcare==1;
replace iprem_b=iprem if iprem_b==. & dead==0;

* medcost_b includes out of pocket payments for part B Medicare, medcost_bmedicaid includes what Medicaid pays for part B Medicare;
gen medcost_bmedicaid=medcost+ptBprem if (medhmo==0|medhmo==.) & medcare==1; //medcost w/ part B if on medicare, but neither medicaid nor medicare HMO;
replace medcost_bmedicaid=medcost if medcost_bmedicaid==. & dead==0; 

*premium if half in last period of life;
sort HHID PN wave;
replace medcost_b=medcost+ptBprem/2 if died==1 & (medicaid==0|(medicaid==. & medicaid[_n-1]==0)) & (medhmo[_n-1]==0|medhmo[_n-1]==.) & medcare[_n-1]==1;
replace medcost_b=medcost if died==1 & medcost_b==.;
replace iprem_b=iprem+ptBprem/2 if died==1 & (medicaid==0|(medicaid==. & medicaid[_n-1]==0)) & (medhmo[_n-1]==0|medhmo[_n-1]==.) & medcare[_n-1]==1;
replace iprem_b=iprem if died==1 & iprem_b==.;
replace medcost_bmedicaid=medcost+ptBprem/2 if died==1 & (medhmo[_n-1]==0|medhmo[_n-1]==.) & medcare[_n-1]==1;
replace medcost_bmedicaid=medcost if died==1 & medcost_bmedicaid==.;
*In the cases where died is incomplete;
replace medcost_b=medcost if medcost_b==.;
replace iprem_b=iprem if iprem_b==.;
replace medcost_bmedicaid=medcost if medcost_bmedicaid==.;


gen _ptBprem=.;
gen _medcost_b=.;
gen _iprem_b=.;
gen _medcost_bmedicaid=.;

*assign spousal variables;
sort HHID wave sub;
replace _ptBprem=ptBprem[_n-1] if HHID==HHID[_n-1] & sub==sub[_n-1] & wave==wave[_n-1];
replace _medcost_b=medcost_b[_n-1] if  HHID==HHID[_n-1] & sub==sub[_n-1] & wave==wave[_n-1];
replace _medcost_bmedicaid=medcost_bmedicaid[_n-1] if  HHID==HHID[_n-1] & sub==sub[_n-1] & wave==wave[_n-1];
replace _iprem_b=iprem_b[_n-1] if  HHID==HHID[_n-1] & sub==sub[_n-1] & wave==wave[_n-1];
replace _ptBprem=ptBprem[_n+1] if HHID==HHID[_n+1] & sub==sub[_n+1] & wave==wave[_n+1];
replace _medcost_b=medcost_b[_n+1] if  HHID==HHID[_n+1] & sub==sub[_n+1] & wave==wave[_n+1];
replace _medcost_bmedicaid=medcost_bmedicaid[_n+1] if  HHID==HHID[_n+1] & sub==sub[_n+1] & wave==wave[_n+1];
replace _iprem_b=iprem_b[_n+1] if  HHID==HHID[_n+1] & sub==sub[_n+1] & wave==wave[_n+1];

drop died;

*replace assets=vla; * this is for considering the case of liquid assets in "The Effects of Health Insurance and Self Insurance";


* Chris test summaries;
cls;
sort wave;
by wave: sum realyear assets;
by wave: summ hospc nursc outsgc docc dentc drugc hhsc othels;
by wave: summ _hospc _nursc _outsgc _docc _dentc _drugc _hhsc _othels;	

save "`saved'merge5", replace;
* drop _all;
program drop _all;
log close ;

